'use strict';

/* Controllers */


function homeCtrl($scope,$http,$timeout) {
	
	$http.get('trendingCategories').success(function(data) {
		$scope.categories = data;
	});

	var postExpertObj = new Object();
	$http.post('asearch',postExpertObj).success(function(data) {
		$scope.experts = data;
	});

	var postSessionObj = new Object();
	postSessionObj.startTime = new Date();
	$http.post('etsearch',postSessionObj).success(function(data) {
		$scope.sessions = data;
	});

	$scope.article=3;
	
	$scope.searchSkill = undefined;

	$scope.search=function() {
		
		var id=-1;
		 $.each($scope.categories, function (i, cat) {
			if(cat.name.toLowerCase() == $scope.searchSkill.txt.trim().toLowerCase()){
				id=cat.id;
			}	
    	});
		if(id!=-1){
			$scope.searchSkill="";
			$location.path("/scategory/"+id);
		}
};
	
}
homeCtrl.$inject = ['$scope','$http','$timeout'];

function CalCtrl($scope,$http){

    var requests = [];
    var eventDataList = [];
    
    $scope.eventSource = {
            url: "http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic",
            className: 'gcal-event',           // an option!
            currentTimezone: 'America/Chicago' // an option!
    };
    
    $http.get('requests').success(function(data) {
    	
    	requests = eval(data);
    	for(var i = 0; i < requests.length; i++) {
    		var eventData = {};
    		if(requests[i].expert){
    			eventData.title = 'Teach';
    		}else{
    			eventData.title = 'Learn';
    		}
    	
    	eventData.start = new Date(requests[i].startTime).getTime() / 1000;
    	eventData.end = new Date(requests[i].endTime).getTime() / 1000;
    	eventData.className = ['customFeed'];
    	eventData.allDay= false
    	
    	eventDataList.push(eventData);
    	
    	}
    	
});
    $scope.events = eventDataList;
    
	$scope.changeView = function(view) {
      $scope.myCalendar.fullCalendar('changeView',view);
    };
	$scope.uiConfig = {
      calendar:{
        editable: false,
        header:{
          left: 'month agendaWeek agendaDay',
          center: 'title',
          right: 'prev,next'
        },
      	defaultView: 'month'
      }
    };
	
	$scope.eventSources = [$scope.events, $scope.eventSource];
}
CalCtrl.$inject = ['$scope','$http'];

function headerCtrl($scope,$http,$location, $modal,$q,$cookieStore,$route) {
	//Get the profile from session
	nexus.headerScope = $scope;
	//$scope.first=true;
	$scope.notLogin=false;
	
	
	$http.get('checkLogin').success(function(data) {
    	$scope.user = data;
		if ($scope.user === ''){
			$scope.notLogin=true; // Have to set this value if user profile exists in session	
		}else{
			$scope.notLogin=false;
			$scope.userName=$scope.user.name;
		}
		nexus.headerChoice=true;
		$scope.homePage=true;
		
		nexus.headerScope = $scope;

		if(!$scope.notLogin){
			var p = $location.path();
			if(p===''){
				var url = $cookieStore.get('rurl');
				$cookieStore.remove('rurl');
				if (typeof url === "undefined"){
					p='/dashboard/feed';
				}else{
					p=url;
				}
			}/*else{
				if($route.routes[p].view === 'section'){
					p='/dashboard';
				}
			}*/
			$location.url(p);
		}
	});

	$scope.home=function(){
		
		if(!nexus.headerScope.notLogin){
			$location.url('/dashboard/feed');
		}else{
			$location.url('/home');
		}
	/*	
		if(nexus.headerScope.turl  && getType.toString.call(nexus.headerScope.turl ) === '[object Function]'){
			nexus.headerScope.turl();
		}else{
			if(!$scope.notLogin){
				$location.url('/dashboard');
			}else{
				$location.url('/');
			}
		}
		nexus.headerScope.turl=function(){$location.url('/');};
	*/
	}

	$scope.showHomePage=function (){
		$scope.homePage=true;
		$location.url('/home');
	}
	
	$scope.logout=function($http){
		this.notLogin=true;
		
	};

	var modal = $modal({template: 'resources/tpl/login.html', persist: true,show: false, backdrop: 'static',scope: $scope});
	nexus.headerScope.showLogin = function(ploc) {
	 $cookieStore.put('rurl', ploc);
	   $q.when(modal).then(function(modalEl) {
			modalEl.modal('show');
		});
	};

	
}


headerCtrl.$inject = ['$scope','$http','$location','$modal','$q','$cookieStore','$route'];


function loginCtrl($scope,$http,$location) {
	nexus.headerScope.homePage=false;

	$scope.login = function (){

		nexus.headerScope.notLogin = false;
		var getType = {};
		if(nexus.headerScope.turl  && getType.toString.call(nexus.headerScope.turl ) === '[object Function]'){
			nexus.headerScope.turl();
		}
		nexus.headerScope.turl=function(){};
	};
}
loginCtrl.$inject = ['$scope','$http','$location'];
